package rit.edu.cjp5465.simpGrocList;

import android.app.ListActivity;
import android.content.Intent;
import android.database.Cursor;
import android.os.Bundle;
import android.view.View;
import android.widget.ListView;
import android.widget.SimpleCursorAdapter;
import android.widget.TextView;

public class ShoppingList extends ListActivity {
	private DBAdapter db;
	private Cursor listCursor;
	private TextView listName;
	private long listID;
	
	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.shopping_list_view);
		
		Bundle extras = getIntent().getExtras();
		listName = (TextView)findViewById(R.id.listName);
		
		if( extras != null){
			listID = extras.getLong(DBAdapter.CL_ROWID);
			listName.setText( " " + extras.getString(DBAdapter.CL_NAME) );// + " " + extras.getString(DBAdapter.CL_SL_DATE) );			
		}
						
		db = new DBAdapter(this);
		fillData();
		
		registerForContextMenu(getListView());
	}
	
	private void fillData(){
		 // Get all of the notes from the database and create the item list
		db.open();
       listCursor = db.getAllItems(listID);
       startManagingCursor(listCursor);

       String[] from = new String[] { DBAdapter.CL_NAME };
       int[] to = new int[] { R.id.text1 };
       
       // Now create an array adapter and set it to display using our row
       SimpleCursorAdapter notes =
           new SimpleCursorAdapter(this, R.layout.grocery_item_row, listCursor, from, to);
       setListAdapter(notes);
       db.close();
	}
	
	@Override
	protected void onListItemClick (ListView l, View v, int position, long id) {
		this.startActivity(new Intent().setClass(this, ShoppingItem.class));
	}

}
